﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;


namespace Project.Teller
{
    public partial class CreateOrder : System.Web.UI.Page
    {

        DataPOFDataContext db;
        protected void Page_Load(object sender, EventArgs e)
        {


            DataPOFDataContext db = new DataPOFDataContext();
            string acc = (from x in db.AccountLogins where x.Username == Session["username"].ToString() select x.FullName).FirstOrDefault();
            Label1.Text = acc;
            int ID = (from x in db.OrderDetails select x.Id).Count();
            TextBox1.Text = Convert.ToString(ID + 1);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["username"] = null;
            Response.Cache.SetNoStore();
            Response.CacheControl = "no-cache";
            Response.Redirect("../Home/Login.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string testphone = null;
            string testcard = null;
            Label11.Text = null;
            Label12.Text = null;
            Label13.Text = null;
            DataPOFDataContext data2 = new DataPOFDataContext();
            double n = 0;


            OrderDetail orderDetail = new OrderDetail();
            if ((TextBox6.Text == "") || (TextBox1.Text == "") || (TextBox2.Text == "") || (TextBox3.Text == "") || (TextBox4.Text == "") || (TextBox5.Text == "") || (TextBox7.Text == "") || (Label5.Text == "0") || (Label5.Text == null))
            { Label11.Text = "Insert data on (*)"; }
            else
            {
                string s = TextBox4.Text;
                string p = TextBox7.Text;
                testphone = s.Substring(0, 1);
                testcard = p.Substring(0, 1);
                if ((double.TryParse(this.TextBox4.Text, out n)) && (testphone != "-"))
                {
                    if ((double.TryParse(this.TextBox7.Text, out n)) && (testcard != "-"))
                    {
                        ///test/////
                        string route = DropDownList1.SelectedItem.ToString() + DropDownList2.SelectedItem.ToString();
                        int routid = (from x in data2.Routes where x.Route1 == route select x.ID).FirstOrDefault();
                        ///

                        // Add RouteID
                        orderDetail.Id = Convert.ToInt32(TextBox1.Text);
                        orderDetail.CustomerOrderID = Convert.ToInt32(DropDownList4.SelectedItem.ToString());

                        orderDetail.RouteID = routid;
                        routid = 1;
                        int number2 = 0;
                        if (DropDownList3.SelectedValue.ToString().Equals("normal"))
                        {
                            number2 = 1;
                        }
                        else if (DropDownList3.SelectedValue.ToString().Equals("fast"))
                        {
                            number2 = 2;
                        }

                        int idAccount = (from x in data2.AccountLogins where x.FullName == Label8.Text select x.Id).FirstOrDefault();
                        orderDetail.SeviceID = number2;
                        number2 = 0;
                        orderDetail.AccountID = idAccount;

                        orderDetail.ReceiverName = TextBox2.Text;

                        orderDetail.ReceiverIdentity = TextBox7.Text;

                        orderDetail.ReceiverAddress = TextBox3.Text;

                        orderDetail.ReceiverPhone = Convert.ToInt32(TextBox4.Text);

                        orderDetail.ReceiverEmail = TextBox5.Text;

                        orderDetail.Volume = Convert.ToInt32(TextBox6.Text);

                        orderDetail.Price = Convert.ToDecimal(Label5.Text);

                        //////////



                        // Add

                        try
                        {

                            data2.OrderDetails.InsertOnSubmit(orderDetail);

                        }

                        catch (Exception ex)
                        {

                            //MessageBox.Show(ex.Message);

                        }

                        finally
                        {

                            // Submit to data

                            data2.SubmitChanges();
                            Response.Redirect("../Teller/CreateOrder.aspx");
                        }
                    }
                    else { Label13.Text = "Please enter a positive number"; }
                }
                else { Label12.Text = "Please enter a positive number"; }
            }
        }

        protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();
            string route = DropDownList1.SelectedItem.ToString() + DropDownList2.SelectedItem.ToString();
            int routid = (from x in db.Routes where x.Route1 == route select x.ID).FirstOrDefault();

            int Distance = (from x in db.RouteDetails where x.ID == routid select x.Distance).FirstOrDefault();
            Label1.Text = Distance.ToString();
            decimal DistancePrice = (from x in db.PriceOfVolumes where x.ID == 1 select x.Distance).FirstOrDefault();
            int a = Convert.ToInt32(DistancePrice) * Distance;

            if (Label1.Text == "0")
            {
                Label2.Text = "0";
                double b = Convert.ToDouble(Label3.Text);
                double c = Convert.ToDouble(Label4.Text);
                Label5.Text = Convert.ToString(c + b);
            }
            else
            {
                Label2.Text = a.ToString();
                double b = Convert.ToDouble(Label3.Text);
                double c = Convert.ToDouble(Label4.Text);
                Label5.Text = Convert.ToString(a + b + c);
            }
        }

        protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();

            int LocationID = (from x in db.Locations where x.Location1 == DropDownList2.SelectedItem.ToString() select x.ID).FirstOrDefault();
            string NameTeller = (from x in db.AccountLogins where x.LocationID == LocationID select x.FullName).FirstOrDefault();
            Label8.Text = NameTeller;

            string route = DropDownList1.SelectedItem.ToString() + DropDownList2.SelectedItem.ToString();
            int routid = (from x in db.Routes where x.Route1 == route select x.ID).FirstOrDefault();
            int Distance = (from x in db.RouteDetails where x.ID == routid select x.Distance).FirstOrDefault();


            Label1.Text = Distance.ToString();
            decimal DistancePrice = (from x in db.PriceOfVolumes where x.ID == 1 select x.Distance).FirstOrDefault();
            int a = Convert.ToInt32(DistancePrice) * Distance;
            if (Label1.Text == "0")
            {
                Label2.Text = "0";
                double b = Convert.ToDouble(Label3.Text);
                double c = Convert.ToDouble(Label4.Text);
                Label5.Text = Convert.ToString(c + b);
            }
            else
            {
                Label2.Text = a.ToString();
                double b = Convert.ToDouble(Label3.Text);
                double c = Convert.ToDouble(Label4.Text);
                Label5.Text = Convert.ToString(a + b + c);
            }

        }

        protected void DropDownList3_SelectedIndexChanged1(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();
            string z = DropDownList3.SelectedValue.ToString();
            decimal ServicePrice = (from x in db.Services where x.Service1 == z select x.Price).FirstOrDefault();
            Label4.Text = Convert.ToString(ServicePrice);
            double a = Convert.ToDouble(Label2.Text);
            double b = Convert.ToDouble(Label3.Text);
            double c = Convert.ToDouble(ServicePrice);
            Label5.Text = Convert.ToString(a + b + c);
        }

        protected void TextBox6_TextChanged(object sender, EventArgs e)
        {
            Label9.Text = null;
            Label5.Text = null;
            double n = 0;
            string s = TextBox6.Text;

            Label10.Text = s.Substring(0, 1);
            if ((double.TryParse(this.TextBox6.Text, out n)) && (Label10.Text != "-"))
            {
                DataPOFDataContext db = new DataPOFDataContext();
                decimal Distance = (from x in db.PriceOfVolumes select x.Volume).FirstOrDefault();
                decimal z = 0;

                if (Convert.ToDouble(TextBox6.Text) < 1)
                {
                    z = Convert.ToDecimal(TextBox6.Text) * Distance;
                    Label3.Text = z.ToString();
                    double a = Convert.ToDouble(Label2.Text);
                    double c = Convert.ToDouble(Label4.Text);
                    Label5.Text = Convert.ToString(a + 1000 + c);
                }
                else
                {
                    z = Convert.ToDecimal(TextBox6.Text) * Distance;
                    Label3.Text = z.ToString();
                    double a = Convert.ToDouble(Label2.Text);
                    double c = Convert.ToDouble(Label4.Text);
                    Label5.Text = Convert.ToString(a + c + Convert.ToDouble(z));
                }

            }
            else { Label9.Text = "Please enter a positive number"; Label5.Text = null; }


        }

        protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();
            string Name = (from x in db.CustomerOrders where x.ID == Convert.ToInt32(DropDownList4.SelectedItem.ToString()) select x.Name).FirstOrDefault();
            Label7.Text = Name;

        }

        protected void TextBox4_TextChanged(object sender, EventArgs e)
        {
            double n = 0;
            string s = TextBox4.Text;
            Label10.Text = s.Substring(0, 1);
            if ((double.TryParse(this.TextBox4.Text, out n)) && (Label10.Text != "-"))
            {

            }
            else { Label12.Text = "Please enter a positive number"; }
        }
    }

}